home *** CD-ROM | disk | FTP | other *** search
- (* compare - Binary file comparison program. This program is just a demon-
- stration of file access in Personal Pascal. In the current version, single
- byte access to a file is not that fast, so to increase the speed of this
- program, you might replace the single-byte accesses with block-oriented
- GEMDOS reads into a buffer. The algorithm for comparison is very simple;
- keep track of the current offset position within the two files, get a pair
- of bytes to compare, and print any that don't match. If one file is
- shorter than the other, report that fact, too. Since we are just showing
- binary file access here, there is no GEM interface (if we were selling the
- program, you can bet there would be!). *)
-
- PROGRAM Compare ;
-
- CONST
- max_fn = 80 ; (* Maximum length of a TOS file name *)
-
- TYPE
- file_name = STRING [ max_fn ] ; (* A string big enough to hold a name *)
- byte_file = PACKED FILE OF byte ; (* The declaration for a binary file *)
-
- VAR
- f1, f2 : byte_file ; (* The buffer variables for the two files we *)
- (* will be accessing. *)
- name1, name2 : file_name ; (* And their names *)
-
- (* compare_files - This routine does the actual byte-for-byte compare,
- assuming that the two file buffer variable parameters have already been
- opened to the two files to compare. *)
-
- PROCEDURE compare_files( VAR f1, f2 : byte_file ) ;
-
- VAR
- c1, c2 : byte ; (* These will hold a pair of bytes to compare *)
- offset : long_integer ; (* Current offset within the two files *)
- errors : boolean ; (* True if non-matching bytes have been found *)
-
- BEGIN
- offset := 0 ; (* We start at the beginning with no errors *)
- errors := false ;
- (* While we aren't at the end of either file, get a pair of bytes and
- compare them. If they are not equal, print out the current offset,
- and the two values. Also set the 'errors' flag to true, so we will
- say at the end that the files don't match. *)
- WHILE NOT eof(f1) AND NOT eof(f2) DO
- BEGIN
- c1 := f1^ ; get( f1 ) ;
- c2 := f2^ ; get( f2 ) ;
- IF c1 <> c2 THEN
- BEGIN
- errors := true ;
- writeln( offset:6:h, ': ', c1:2:h, ' ', c2:2:h ) ;
- END ;
- offset := offset + 1 ;
- END ;
- (* If we reached the end of one file, but not the other, tell the user
- which file is shorter. *)
- IF eof(f1) AND NOT eof(f2) THEN
- writeln( 'File 1 is shorter' )
- ELSE IF eof(f2) AND NOT eof(f1) THEN
- writeln( 'File 2 is shorter' )
- (* If neither is shorter, and we never got a non-matching pair, tell the
- user the files are the same. *)
- ELSE IF NOT errors THEN
- writeln( 'Files compare exactly' ) ;
- END ;
-
- (* main routine - Ask the user for the names of the two files to compare, open
- the files, and call 'compare_files' to do the real work. We are not
- getting the parameters from a command line so the program can be called
- from the desktop or the Pascal manager. Those of you using command-line
- interfaces might want to pull the parameters from the command line,
- instead. *)
-
- BEGIN
- write( 'File 1: ' ) ;
- readln( name1 ) ;
- reset( f1, name1 ) ;
- write( 'File 2: ' ) ;
- readln( name2 ) ;
- reset( f2, name2 ) ;
- compare_files( f1, f2 ) ;
- END.
- {{{{{